package com.pdf.pdfserver.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @Author songfz
 * @Description 实现对Spring上下文数据的统计或者监控
 * @Date 2022/2/7
 */
@Component
public class StatisticsContextListener {

    private static Logger LOG = LoggerFactory.getLogger(StatisticsContextListener.class);
    @EventListener
    public void contextCount(ContextRefreshedEvent event){

        ApplicationContext applicationContext = event.getApplicationContext();
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        Arrays.asList(beanDefinitionNames).stream().forEach(System.out::println);
        LOG.info("统计应用上下文：【{}】",beanDefinitionNames.length);
    }
}
